import java.util.Arrays;

public class Solution1160 {
    public int countCharacters(String[] words, String chars) {
        int[] count=new int[26];
        for (char c : chars.toCharArray()) {
            count[c-'a']++;
        }
        int ans=0;
        for (String word : words) {
            int[] copy= Arrays.copyOf(count,26);
            boolean flag=true;
            for (char c : word.toCharArray()) {
                copy[c-'a']--;
                if (copy[c-'a']<0){
                    flag=false;
                    break;
                }
            }
            if (flag){
                ans+=word.length();
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1160().countCharacters(new String[]{"hello","world","leetcode"},"welldonehoneyr"));
    }
}
